package com.ac.scum.log.web.model.server.mapper;

import com.ac.scum.log.bean.Server;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ServerMapper extends BaseMapper<Server> {


    @Insert("insert into server_admin_user values(#{serverId},#{userId})")
    void insertServerAsAdminUser(@Param("serverId") Long serverId, @Param("userId") Long userId);


    default void save(Server server, Long userId) {
        insert(server);
        insertServerAsAdminUser(server.getId(), userId);
    }

    @Select("select user_id from server_admin_user where server_id = #{serverId}")
    List<Long> getUserIdsByServerId(@Param("serverId") Long serverId);

    @Select("select server_id from server_admin_user where user_id = #{userId}")
    List<Long> getServerIdsByUserId(@Param("userId") Long userId);

    @Select("select s.* from server s inner join server_package sp on sp.server_id = s.id where sp.package_id = 1793235135389839362 and sp.expiration_time > now() and ${where}")
    List<Server> getHasAlarmServersByWhere(@Param("where") String where);
}
